//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        tabs: ["进行中", "已完成"],
        status:[2,1],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        motto: 'Hello World',
        isClick:0,
        orderList0:[],
        orderList1:[],
        orderList2:[],
        orderList3:[],
        list:['orderList0','orderList1','orderList2','orderList3'],
        returnPage:'',
        userType:'',
        sliderWidth:0
    },
    onLoad: function () {

    },
    getRect: function (index) {
        var that = this;
        setTimeout(function () {
            wx.createSelectorQuery().select('#slider-item-'+index).boundingClientRect(function (rect) {
                that.setData({
                    sliderWidth: rect.width,
                    sliderOffset:rect.left
                })
            }).exec();
        },10)
    },
    tabClick: function (e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex:index
        });
        this.manageOrderList()
    },
    onShow:function(){
        var userType = wx.getStorageSync('userType');
        var eqa = (userType == this.data.userType);
        if(!userType){
            this.setData({
                tabs:["未接单", "进行中", "已完成","已取消"],
                status:[3,2,1,4],
                userType:0
            })
        }else{
            this.setData({
                tabs:[ "进行中", "已完成"],
                status:[2,1],
                userType:1
            })
        }
        if(!eqa) {
            this.setData({
                activeIndex:0
            });
        }
        this.getRect(this.data.activeIndex);
        this.manageOrderList()
    },
    manageOrderList:function(){
        var index = this.data.activeIndex;
        if(!this.data.userType){
            this.getOrderList(index)
        }else{
            this.getRunOrderList(index)
        }
    },
    //普通用户的单子
    //3订单已经完成（生成）2订单进行中1订单已经完成生成（送货到家了）4订单已经取消
    getOrderList:function (index) {
        var that = this;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        var status = that.data.status[index]
        var key = that.data.list[index]
        var obj = {};
        obj[key] = '';
        app.util.request({
            url:app.globalRequestUrl + '/orderlist',
            method:'GET',
            data:{
                status:status||'1'
            },
            success:function (res) {
                if(res && res.data.length){
                    obj[key] = res.data;
                    that.setData(obj)
                }
            },
            complete:()=>  {
                wx.hideToast()
            }
        })
    },
    //跑腿的单
    //status  （空是全部  1已经完成 2订单进行中）
    getRunOrderList:function (index) {
        var that = this;
        wx.showToast({
            icon:'loading',
            title:'加载中'
        })
        var status = that.data.status[index]
        var key = that.data.list[index]
        var obj = {};
        obj[key] = [];
        app.util.request({
            url:app.globalRequestUrl + '/runorderlist',
            method:'GET',
            data:{
                status:status||''
            },
            success:function (res) {
                if(res && res.data.length){
                    obj[key] = res.data;
                    that.setData(obj)
                }
            },
            complete:()=>  {
                wx.hideToast()
            }
        })
    },
    //普通用户取消订单
    bindOrderCancel:function (e) {
        var that = this;
        wx.showToast({
            title:'加载中！',
            icon:'loading'
        });
        var id = e.currentTarget.dataset.id;
        var data = {
            o_id:id
        }
        app.util.request({
            url: app.globalRequestUrl + '/setorderno',
            method:'GET',
            data:data,
            success: function (res) { //调起微信支付
                if(res && res.msg == 'success'){
                    wx.showModal({
                        title:'提示',
                        content:'取消订单成功！',
                        showCancel:false
                    });
                    that.manageOrderList()
                }
            },
            fail: function(){
            },
            complete: function(){
                wx.hideToast();
            }
        });
    },
    //普通用户的确认订单，跑腿人员订单里没有操作选项，普通用户有取消（未接单时）和完成
    bindOrderFinish:function (e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var that = this;
        if(that.data.isClick) return;
        that.data.isClick = 1;

        app.util.request({
            url:app.globalRequestUrl + '/reseipt',
            method:'GET',
            data:{
                o_id:id
            },
            success:function (res) {
                if(res && res.msg == 'success'){
                    wx.showToast({
                        title:'收货成功！',
                        icon:'success'
                    });
                    that.manageOrderList()
                }
            },
            complete:function () {
                setTimeout(function () {
                    that.data.isClick = 0;
                },1000)
            }
        })
    },
})